package cn.haohaowo;

import cn.haohaowo.entity.Book;
import cn.haohaowo.service.BookService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by luwancai on 18/5/19.
 * DESC:
 */
@Slf4j
@RestController
public class BookController {

    @Autowired
    private BookService bookService;

    @RequestMapping("/queryBookById")
    public String queryBookById(String bookId) {

        log.info("Param bookId: {}", bookId);
        String result = null;
        try {
            Book book = bookService.queryBookById(Integer.valueOf(bookId));
            if (null != book) {
                result = JSONObject.toJSONString(book);
                log.info("Return result: {}", result);
            }
        } catch (Exception e) {
            log.error("Service queryBookById error: {}", e.getMessage(), e);
        }

        return result;

    }


    @RequestMapping("/queryBooks")
    public String queryBooks(String type, String title, String author,
                             String publisher, String minPrice, String maxPrice,
                             int pageNo, int pageSize) {

        log.info("Param type: {}, title: {}, author: {}, publisher: {}, " +
                "minPrice: {}, maxPrice: {}, pageNo: {}, pageSize: {}",
                type, title, author, publisher, minPrice, maxPrice, pageNo, pageSize);
        String result = null;
        try {
            int minRow = (pageNo - 1) * pageSize;
            List<Book> bookList = bookService.queryBooks(type, title, author,
                    publisher, Integer.valueOf(minPrice), Integer.valueOf(maxPrice),
                    minRow, pageSize);
            if (null == bookList && !bookList.isEmpty()) {
                JSONArray jsonArray = new JSONArray();
                jsonArray.addAll(bookList);
                result = jsonArray.toJSONString();
                log.info("Return result: {}", result);
            }
        } catch (Exception e) {
            log.error("Service queryBooks error: {}", e.getMessage(), e);
        }

        return result;

    }

    @RequestMapping("/queryHotBooks")
    public String queryHotBooks() {

        String result = null;
        try {
            List<Book> bookList = bookService.queryHotBooks();
            if (null == bookList && !bookList.isEmpty()) {
                JSONArray jsonArray = new JSONArray();
                jsonArray.addAll(bookList);
                result = jsonArray.toJSONString();
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("Service queryHotBooks error: {}", e.getMessage(), e);
        }

        return result;

    }

    @RequestMapping("/querySearchBooks")
    public String querySearchBooks(String keyword) {

        log.info("Param keyword: {}", keyword);
        String result = null;
        try {
            List<Book> bookList = bookService.querySearchBooks(keyword);
            if (null == bookList && !bookList.isEmpty()) {
                JSONArray jsonArray = new JSONArray();
                jsonArray.addAll(bookList);
                result = jsonArray.toJSONString();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;

    }

}
